package frame.laxcen.response;

import frame.laxcen.BaseFrame;
import frame.laxcen.util.FrameReader;
import frame.laxcen.exception.IllegalFrameFormatException;
import util.ByteUtil;

import java.util.HashMap;
import java.util.Map;

// 返回读写器信息
public class Response_0001_00 extends BaseFrame implements IResponse {

    public Response_0001_00(BaseFrame baseFrame) throws IllegalFrameFormatException {

        initialize(baseFrame.FrameBytes);
    }

    public Response_0001_00(byte[] bytes) throws IllegalFrameFormatException {

       initialize(bytes);
    }

    @Override
    public Map<String, String> explainToMap() throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        try {

            FrameReader reader = new FrameReader(this);

            // 应用处理器软件版本

            byte[] version = ByteUtil.intToBytes(reader.readU32());

            result.put("应用处理器软件版本", String.format("V %d.%d.%d", version[1], version[2], version[3]));

            // 读写器名称

            byte[] name = reader.readU8s();

            result.put("读写器名称", new String(name));

            // 读写器上电时间

            long time = reader.readU32() & 0xffffffffL;

            result.put("读写器上电时间", time + "秒");

        } catch (Exception e) {

            e.printStackTrace();

            throw new IllegalFrameFormatException(e.getMessage());
        }

        return result;
    }
}
